package com.chebaohui.iqs.service.impl.deal.taiping;

import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/****
 * 配置信息
 * 
 * @author xielonghai
 * @version 2016-06-30
 *
 */
public class PublicNew {

	public static final boolean test = false;

	/** 回调地址 **/
	public static String getRollBackUrl() {
		if (test)
			return "http://chebaohui.regas.tunnel.qydev.com/";
		else
			return "http://online.taiping.chebaohui.com/";
	}

	/**
	 * 获取访问地址
	 * 
	 * @return
	 */
	private static String getURL() {
		if (test)
			return "https://car.etaiping.com:6004/";
		else
			return "https://tpadapter.tpi.cntaiping.com:8001/";
	}

	/** 报文ID--String--32-38---合作方生成 需保证唯一性-----Y **/
	public static final String msgId = "chongqingtaipinghrnpfs";// chongqing2016-huarui-docking-taiping

	/** 用户ID--40--合作方生成 需保证唯一性---------N **/
	public static final String userCode = "cq-hrnpfs";

	/** 编码方式 **/
	public static final String defaultCharset = "UTF-8";

	/** 商户名称 --商户号 **/
	public static final String clientName = "S010001";// S010001 //S003001

	/** 团队号 **/
	public static final String groupNo = "HRCQ001";// HRCQ001 //CQANSHUN001

	/** 子协议号 **/
	public static final String solutionCode = "0351010601002000040004";// 0351010601002000040004/*/0351010601002000040005
																		// //0351010601002000040005

	/** 决策单元代码 **/
	public static final String salesmanCode = "5100000723";// 5100000723
															// //5100000044

	/** 密钥-生成方式 **/
	public static final String algorithm = "SHA256WITHRSA";

	/** 太平直连接口地址--https://car.etaiping.com:6004/ecms2/portal/商户号/请求类型（接口名称） */
	public static String REQUEST_URL = getURL() + "ecms2/portal/" + clientName;

	/** 页数 */
	public static final int PAGE = 1;

	/** 每页条数(默认20条) */
	public static final int COUNT = 20;

	/** 保险公司公钥 */
	public static final String tp_public = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo8c5E0bT05P2zdCYJ1MOjc+t3Yy+wYdatG1IpUF1ZpcaSxPUFLleYN1kMdHssxgeW8ujv8dns5UaMJyYw6arWch78+9SZFzM9BEwM7JD8BcPFFMUqSgVxQ/kKs+brDm8cdeEI3kQz906EhygR1j23HJzfs+Gr2nFiEs5/XyOP9BRQ6S8yK9B2zBBnHXkvf8rIDkOjcMTOUxpfmEm17RDN2a5C/wd0yC4lIHUrQfkYaUQVOZu53gBB4a+UYVlISRqKL0XoVO5KFWpfei9NtTZoaeHVWeMMPWOQJyGvd1EXhdsbYRXsdWeQF5uQPM/SGcMUO4CsdGob8lgAi9OaZqjgQIDAQAB";

	/**
	 * 获取公司环境私钥
	 * 
	 * @return
	 */
	public static String getPrivateKey() {
		if (test)
			return cs_private;
		else
			return zs_private;
	}

	/** 测试环境公钥 */
	public static final String cs_public = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl5dPYQhe6LZQ0epXVWU4p4xzqN498AWrkC8Y0NlxvYOARdjjYhwJqYwgPnjt3xQT2TR/CzPtmTAPrKPIAKCvwMlQSPW053VzTQysTGVD3qSCNQ02iXb10TeU59D8O9OVA9sh9zN57WosWCkBJnXjZvBjjy51KtpKrj4VAyVwYjDb62LcAyJnCKMmq3iL1gmqnb1lhpF0xCNuwBvJPJkVy2p3zZzRiST9cxpcmQ/z02PI1yW7meqEeOWMxcaHWGCaQov4V5/QJ8XRFR8YHGuN+h8eEbZntl7TkAf9aX2jNw1OAs6zQyg9LVQARhqcIORiCGXWeSqmn3oBTP4V5gtacwIDAQAB";

	/** 测试环境私钥 */
	private static final String cs_private = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCXl09hCF7otlDR6ldVZTinjHOo3j3wBauQLxjQ2XG9g4BF2ONiHAmpjCA+eO3fFBPZNH8LM+2ZMA+so8gAoK/AyVBI9bTndXNNDKxMZUPepII1DTaJdvXRN5Tn0Pw705UD2yH3M3ntaixYKQEmdeNm8GOPLnUq2kquPhUDJXBiMNvr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FEQer6QQ5HrY4NDWwEqZbiNuN8aGsdyvZ2ZXvEsi9DhU7FToahktFSXoTYLU3SbfVTfu7y6FlxZkqRM0EoBW2zg3rKF7GJrDXNFfb8NfmJ9+3WZGjs94EmCQ1hChlTuw1cCgYBBe7JBnG3P6+5391MCI61Oy+kwfBJkKlWLoNRH6ww1VC8S1rCcQAPC4kCzWzLhoOJMKsHyAD/qffk2IVvSv6gRghpsvuXmn5OevZ6Ag2QrTkTlOqShA/13k+Z9XxtIX0GHURMSO9v088C/y+oGVHLmzJoi5pp2yQIgc/LjdsCyeA==";

	/** 生产环境公钥 */
	public static final String zs_public = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy4vSyiDVoTfUtqwQlRQtYmTsmiOuXQlYbeiqdQsNt/yQTdWmzcptwzPV5IJTa0avSoPQbsyPyCvmzL+iQUG5DygwGfV+Gvml13dGIIiUjPrD9ybi6vDnDWkdx3wVPeJiYY13PJJLkh8gYkM7LklAWhG4dO8iRoTbBAGXsCqeyP1pbpPN7HMxx2dsuluvd1x2/KoV/b2S7Rp4jkidkYDFNak+mGI9CjPBDU0bdHsSCdyabk+KNIsKyaYIrc3DMjYPo37VoweXsXAXr64MwHHhyHff7O8+gFFdopbWf8Jypw0Wj55+CJcaV0KIAiSXkuiqTtB2HDLMhoFSfrqQtnoWaQIDAQAB";

	/** 生产环境私钥 */
	private static final String zs_private = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDLi9LKINWhN9S2rBCVFC1iZOyaI65dCVht6Kp1Cw23/JBN1abNym3DM9XkglNrRq9Kg9BuzI/IK+bMv6JBQbkPKDAZ9X4a+aXXd0YgiJSM+sP3JuLq8OcNaR3HfBU94mJhjXc8kkuSHyBiQzsuSUBaEbh07yJGhNsEAZewKp7I/Wluk83sczHHZ2y6W693XHb8qhX9vZLtGniOSJ2RgMU1qT6YYj0KM8ENTRt0exIJ3JpuT4o0iwrJpgitzcMyNg+j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GIfdE8lFpmp1r9QLyRIEj4NkFRprRvVKCuI6xkS9GchHtvFBVW0QKBgH6F2l9cMuQzd6M6zIHN5aD1GWfisQlJFCALHWwims1A4i/c8pYYcLpkL+Pr8Byvpfgf+7VVyhaKzmnRhxxz9Hi3XrtBvDravnPc6BLvdVa9M0o9Sy2CTkjmoBOYHDh4BVdSlieWfCO4tT1mc4ft5ZLw3V1GqTFenjv6Qi8cFLeK";

	/** 签名 **/
	public static byte[] sign(final byte[] text, final byte[] privateKeyData, final String algorithm)
			throws GeneralSecurityException {
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyData);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
		Signature signatureChecker = Signature.getInstance(algorithm);
		signatureChecker.initSign(privateKey);
		signatureChecker.update(text);
		return signatureChecker.sign();
	}

	/** 验证签名 **/
	public static boolean verify(final byte[] text, final byte[] signedText, final byte[] publicKeyData,
			final String algorithm) throws GeneralSecurityException {
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyData);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PublicKey publicKey = keyFactory.generatePublic(keySpec);
		Signature signatureChecker = Signature.getInstance(algorithm);
		signatureChecker.initVerify(publicKey);
		signatureChecker.update(text);
		return signatureChecker.verify(signedText);
	}
}
